package cn.aitrox.ry.app.web.controller.auth;

import cn.aitrox.ry.app.web.vo.auth.permission.out.AuthPermissionDetailOutVo;
import cn.aitrox.ry.app.web.vo.auth.role.in.AuthRoleCreateInVo;
import cn.aitrox.ry.app.web.vo.auth.role.in.AuthRoleSearchInVo;
import cn.aitrox.ry.app.web.vo.auth.role.in.AuthRoleUpdateInVo;
import cn.aitrox.ry.app.web.vo.auth.role.out.AuthRoleCreateOutVo;
import cn.aitrox.ry.app.web.vo.auth.role.out.AuthRoleDetailOutVo;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleCreateInDto;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleSearchInDto;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleUpdateInDto;
import cn.aitrox.ry.service.auth.dto.role.out.AuthRoleDetailDto;
import cn.aitrox.ry.service.auth.feign.AuthRoleFeign;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AuthRoleController {

    @Autowired
    @Lazy
    private AuthRoleFeign authRoleFeign;

    @PostMapping(value = "/auth/role")
    RespResult<AuthRoleCreateOutVo> create(@Validated @RequestBody AuthRoleCreateInVo input) {
        RespResult<Integer> respResult = authRoleFeign.create(new AuthRoleCreateInDto(input.getName(), input.getPermissionIds()));
        if (!respResult._isOk()) {
            return RespResult.secByError(respResult.getCode(), respResult.getMsg());
        }
        return RespResult.ok(new AuthRoleCreateOutVo(respResult.getData()));
    }

    @PutMapping(value = "/auth/role")
    RespResult update(@Validated @RequestBody AuthRoleUpdateInVo input) {
        return authRoleFeign.update(new AuthRoleUpdateInDto(input.getId(), input.getName(), input.getPermissionIds()));
    }

    @GetMapping(value = "/auth/role{id}")
    RespResult<AuthRoleDetailOutVo> read(@PathVariable("id") Integer id) {
        RespResult<AuthRoleDetailDto> respResult = authRoleFeign.read(id);
        if (!respResult._isOk()) {
            return RespResult.secByError(respResult.getCode(), respResult.getMsg());
        }

        AuthRoleDetailDto roleDetail = respResult.getData();

        List<AuthPermissionDetailOutVo> permissionDetailOutVoList = null;
        if (!ArrayUtil.isEmpty(roleDetail.getPermissionList())) {
            permissionDetailOutVoList = roleDetail.getPermissionList().stream().map(x -> new AuthPermissionDetailOutVo(x.getId(), x.getName(), null, null, null)).collect(Collectors.toList());
        }

        return RespResult.ok(new AuthRoleDetailOutVo(roleDetail.getId(), roleDetail.getName(), roleDetail.getCreateTime(), roleDetail.getUpdateTime(), permissionDetailOutVoList));
    }

    @DeleteMapping(value = "/auth/role/{id}")
    RespResult delete(@PathVariable("id") Integer id) {
        return authRoleFeign.delete(id);
    }

    @PostMapping(value = "/auth/role/search")
    RespResult<CommonPageInfo<AuthRoleDetailOutVo>> search(@RequestBody AuthRoleSearchInVo input) {
        CommonPageInfo<AuthRoleDetailDto> pageInfo = authRoleFeign.search(new AuthRoleSearchInDto(input.getName()));
        if (!ArrayUtil.isEmpty(pageInfo.getList())) {
            return RespResult.ok(new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), null));
        }

        List<AuthRoleDetailOutVo> list = pageInfo.getList().stream().map(x -> {
            List<AuthPermissionDetailOutVo> permissionDetailOutVoList = null;
            if (!ArrayUtil.isEmpty(x.getPermissionList())) {
                permissionDetailOutVoList = x.getPermissionList().stream().map(y -> new AuthPermissionDetailOutVo(y.getId(), y.getName(), null, null, null)).collect(Collectors.toList());
            }
            return new AuthRoleDetailOutVo(x.getId(), x.getName(), x.getCreateTime(), x.getUpdateTime(), permissionDetailOutVoList);
        }).collect(Collectors.toList());


        return RespResult.ok(new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), list));
    }
}
